﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UIProvider.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge.UI.Forms
{
    #region Using Directives

    using System.ComponentModel;

    using Org.OpenTrader.Framework.Forge.Enums;
    using Org.OpenTrader.Framework.Forge.Interfaces;
    using Org.OpenTrader.Framework.Interfaces.UI;
    using Org.OpenTrader.Framework.LiveObjects.Interfaces;
    using Org.OpenTrader.Framework.UI.Forms;

    #endregion

    /// <summary>
    /// The ui provider.
    /// </summary>
    public class UIProvider : IUIProvider
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes static members of the <see cref="UIProvider"/> class.
        /// </summary>
        static UIProvider()
        {
            // Provider for enums
            var tdpEnum = new EnumTypeDescriptionProvider();
            TypeDescriptor.AddProvider(tdpEnum, typeof(ESector));
            TypeDescriptor.AddProvider(tdpEnum, typeof(ECurrency));

            // Provider for markets
            var tdpMarket = new MarketTypeDescriptionProvider();
            TypeDescriptor.AddProvider(tdpMarket, typeof(IMarket));
            TypeDescriptor.AddProvider(tdpMarket, typeof(Market));
        }

        #endregion

        #region Implemented Interfaces

        #region IUIProvider

        /// <summary>
        /// The create application ui.
        /// </summary>
        /// <param name="application">
        /// The application.
        /// </param>
        /// <returns>
        /// </returns>
        public IUIRootControl CreateApplicationUI(IApplication application)
        {
            if (application.GetType() == typeof(BridgeApplication))
            {
                return CreateBridgeApplicationUI(application);
            }

            if (application.GetType() == typeof(TradingApplication))
            {
                return CreateTradingApplicationUI(application);
            }

            return null;
        }

        /// <summary>
        /// The create splash ui.
        /// </summary>
        /// <param name="task">
        /// The task.
        /// </param>
        /// <param name="title">
        /// The title.
        /// </param>
        /// <returns>
        /// </returns>
        public IUISplash CreateSplashUI(TaskDelegate task, string title)
        {
            return new Splash(task, title);
        }

        #endregion

        #endregion

        #region Methods

        /// <summary>
        /// The create bridge application ui.
        /// </summary>
        /// <param name="application">
        /// The application.
        /// </param>
        /// <returns>
        /// </returns>
        private static IUIRootControl CreateBridgeApplicationUI(IApplication application)
        {
            var feedSvc = application["FeedService"] as IFeedService;
            var orderSvc = application["OrderService"] as IOrderService;
            var dicoSvc = application["DictionaryService"] as IDictionaryService;
            IUIRootControl rootControl = new UIBridgeApplication("Bridge");
            rootControl.AddControl(new UIDictionaryService(dicoSvc, feedSvc, orderSvc), "DictionaryService");
            rootControl.AddControl(new UIFeedService(feedSvc), "FeedService");
            rootControl.AddControl(new UIOrderService(orderSvc), "OrderService");

            return rootControl;
        }

        /// <summary>
        /// The create trading application ui.
        /// </summary>
        /// <param name="application">
        /// The application.
        /// </param>
        /// <returns>
        /// </returns>
        private static IUIRootControl CreateTradingApplicationUI(IApplication application)
        {
            var feedSvc = application["FeedService"] as IFeedService;
            var orderSvc = application["OrderService"] as IOrderService;
            var dicoSvc = application["DictionaryService"] as IDictionaryService;
            var strategySvc = application["StrategyService"] as IStrategyService;

            Singleton<Universe>.Instance = new Universe(dicoSvc, feedSvc, orderSvc);

            IUIRootControl rootControl = new UITradingApplication("Trading");
            rootControl.AddControl(new UIDictionaryService(dicoSvc, feedSvc, orderSvc), "DictionaryService");
            rootControl.AddControl(new UIFeedService(feedSvc), "FeedService");
            rootControl.AddControl(new UIOrderService(orderSvc), "OrderService");
            rootControl.AddControl(new UIStrategyService(strategySvc), "StrategyService");

            rootControl.AddControl(new UIPortfolio(), "Portfolio");
            rootControl.AddControl(new UIManualTrading(dicoSvc.Descriptor.ID), "ManualTrading");

            return rootControl;
        }

        #endregion
    }
}